package org.colin.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.colin.code.BaseResponseCode;
import org.colin.config.ConfigCode;
import org.colin.dictPojo.*;
import org.colin.dictPojo.DSubjectVideo;
import org.colin.mapper.*;
import org.colin.service.DSubjectService;
import org.colin.utils.DataResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 题目表 服务实现类
 * </p>
 *
 * @author LiRuiFeng
 * @since 2021-01-09
 */
@Service
public class DSubjectServiceImpl extends ServiceImpl<DSubjectMapper, DSubject> implements DSubjectService {

    @Autowired
    DSubjectMapper dSubjectMapper;
    
    @Autowired
    DSubjectImageMapper dSubjectImageMapper;

    @Autowired
    DSubjectVideoMapper dSubjectVideoMapper;
    
    @Autowired
    DSubjectRecordingMapper dSubjectRecordingMapper;

    @Autowired
    DSubjectChooseMapper dSubjectChooseMapper;

    @Autowired
    DEntryMapper dEntryMapper;

    
    

    @Transactional
    public boolean addSubject(DSubject dSubject, List<JSONObject> videoList, List<JSONObject> imageList, List<JSONObject> recordingList, List<JSONObject> chooseList) {
        boolean save = false;
        try{
            dSubject.setDeleteMark(false);
            dSubjectMapper.insert(dSubject);

            if(videoList != null && videoList.size() > 0) {
                for (int i = 0; i < videoList.size(); i++) {
                    DSubjectVideo dSubjectVideo = videoList.get(i).toJavaObject(DSubjectVideo.class);
                    dSubjectVideo.setSubjectId(dSubjectVideo.getId());
                    dSubjectVideoMapper.insert(dSubjectVideo);
                }
            }
            if(imageList != null && imageList.size() > 0) {
                for (int i = 0; i < imageList.size(); i++) {
                    DSubjectImage dSubjectImage = imageList.get(i).toJavaObject(DSubjectImage.class);
                    dSubjectImage.setSubjectId(dSubject.getId());
                    dSubjectImageMapper.insert(dSubjectImage);
                }
            }
            if(recordingList != null && recordingList.size() > 0) {
                for (int i = 0; i < recordingList.size(); i++) {
                    DSubjectRecording dSubjectRecording = recordingList.get(i).toJavaObject(DSubjectRecording.class);
                    dSubjectRecording.setSubjectId(dSubject.getId());
                    dSubjectRecordingMapper.insert(dSubjectRecording);
                }
            }
            if(chooseList != null && chooseList.size() > 0) {
                for (int i = 0; i < chooseList.size(); i++) {
                    DSubjectChoose dSubjectChoose = chooseList.get(i).toJavaObject(DSubjectChoose.class);
                    dSubjectChoose.setSubjectId(dSubject.getId());
                    dSubjectChooseMapper.insert(dSubjectChoose);
                }
            }
//            for (int i = 0; i < chooseList.size(); i++) {
//                DSubjectChoose dSubjectChoose = chooseList.get(i).toJavaObject(DSubjectChoose.class);
//                dSubjectChoose.setSubjectId(dSubject.getId());
//                dSubjectChooseMapper.insert(dSubjectChoose);
//            }


            save = true;
        }catch (Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }finally {
            return save;

        }
    }

    @Transactional
    public boolean updateSubject(DSubject dSubject, List<JSONObject> videoList, List<JSONObject> imageList, List<JSONObject> recordingList, List<JSONObject> chooseList) {
        boolean update = false;
        try{
            dSubject.setUpdateTime(LocalDateTime.now());
            dSubjectMapper.updateById(dSubject);

            QueryWrapper<DSubjectVideo> dSubjectVideoQueryWrapper = new QueryWrapper<>();
            dSubjectVideoQueryWrapper.eq("subjectId" , dSubject.getId());
            dSubjectVideoMapper.delete(dSubjectVideoQueryWrapper);

            if(videoList != null && videoList.size() > 0) {
                for (int i = 0; i < videoList.size(); i++) {
                    DSubjectVideo dSubjectVideo = videoList.get(i).toJavaObject(DSubjectVideo.class);
                    dSubjectVideo.setSubjectId(dSubject.getId());
                    dSubjectVideoMapper.insert(dSubjectVideo);
                }
            }

            QueryWrapper<DSubjectImage> dSubjectImageQueryWrapper = new QueryWrapper<>();
            dSubjectImageQueryWrapper.eq("subjectId" , dSubject.getId());
            dSubjectImageMapper.delete(dSubjectImageQueryWrapper);
            if(imageList != null && imageList.size() > 0) {
                for (int i = 0; i < imageList.size(); i++) {
                    DSubjectImage dSubjectImage = imageList.get(i).toJavaObject(DSubjectImage.class);
                    dSubjectImage.setSubjectId(dSubject.getId());
                    dSubjectImageMapper.insert(dSubjectImage);
                }
            }
            QueryWrapper<DSubjectRecording> dSubjectRecordingQueryWrapper = new QueryWrapper<>();
            dSubjectRecordingQueryWrapper.eq("subjectId" , dSubject.getId());
            dSubjectRecordingMapper.delete(dSubjectRecordingQueryWrapper);
            if(recordingList != null && recordingList.size() > 0) {
                for (int i = 0; i < recordingList.size(); i++) {
                    DSubjectRecording dSubjectRecording = recordingList.get(i).toJavaObject(DSubjectRecording.class);
                    dSubjectRecording.setSubjectId(dSubject.getId());
                    dSubjectRecordingMapper.insert(dSubjectRecording);
                }
            }

            QueryWrapper<DSubjectChoose> dSubjectChooseQueryWrapper = new QueryWrapper<>();
            dSubjectChooseQueryWrapper.eq("subjectId" , dSubject.getId());
            dSubjectChooseMapper.delete(dSubjectChooseQueryWrapper);
            if(chooseList != null && chooseList.size() > 0) {
                for (int i = 0; i < chooseList.size(); i++) {
                    DSubjectChoose dSubjectChoose = chooseList.get(i).toJavaObject(DSubjectChoose.class);
                    dSubjectChoose.setSubjectId(dSubject.getId());
                    dSubjectChooseMapper.insert(dSubjectChoose);
                }
            }
            update = true;
        }catch (Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }finally {
            return update;

        }
    }

    @Override
    public HashMap selectSubjectById(Integer id) {
        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        HashMap subject = dSubjectMapper.selectSubjectById(id);
        QueryWrapper<DSubjectImage> imageQueryWrapper = new QueryWrapper<>();
        imageQueryWrapper.eq("subjectId" , id);
        List<Map<String, Object>> imagemaps = dSubjectImageMapper.selectMaps(imageQueryWrapper);
        QueryWrapper<DSubjectVideo> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("subjectId" , id);
        List<Map<String, Object>> videomaps = dSubjectVideoMapper.selectMaps(videoQueryWrapper);
        QueryWrapper<DSubjectRecording> recordingQueryWrapper = new QueryWrapper<>();
        imageQueryWrapper.eq("subjectId" , id);
        List<Map<String, Object>> recomaps = dSubjectRecordingMapper.selectMaps(recordingQueryWrapper);
        QueryWrapper<DSubjectChoose> chooseQueryWrapper = new QueryWrapper<>();
        imageQueryWrapper.eq("subjectId" , id);
        List<Map<String, Object>> chooses = dSubjectChooseMapper.selectMaps(chooseQueryWrapper);
        hashMap.put("subject",subject);
        hashMap.put("imageList",imagemaps);
        hashMap.put("videoList",videomaps);
        hashMap.put("recoList",recomaps);
        hashMap.put("chooses",chooses);
        return hashMap;

    }

    @Override
    public IPage<HashMap> selectSubjectIsPage(Page<HashMap> pageD, String param, Integer type, Integer language, String convType) {
        return dSubjectMapper.selectSubjectIsPage(pageD,  param,  type,  language,  convType);
    }

    @Override
    public Integer deleteBatchSubject(List<Integer> ids) {
        Integer state = 1 ;
        try {

            for (int i = 0; i < ids.size(); i++) {
                Integer id = ids.get(i);
                int i1 = dSubjectMapper.deleteById(id);
                QueryWrapper<DSubjectImage> dSubjectImageQueryWrapper = new QueryWrapper<>();
                dSubjectImageQueryWrapper.eq("subjectId" , id);
                dSubjectImageMapper.delete(dSubjectImageQueryWrapper);

                QueryWrapper<DSubjectVideo> dSubjectVideoQueryWrapper = new QueryWrapper<>();
                dSubjectVideoQueryWrapper.eq("subjectId" , id);
                dSubjectVideoMapper.delete(dSubjectVideoQueryWrapper);

                QueryWrapper<DSubjectRecording> dSubjectRecordingQueryWrapper = new QueryWrapper<>();
                dSubjectRecordingQueryWrapper.eq("subjectId" , id);
                dSubjectRecordingMapper.delete(dSubjectRecordingQueryWrapper);
            }
        }catch (Exception e) {
            state = 2;
            e.printStackTrace();
            System.out.println(e.getMessage());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }finally {
            return state;
        }
    }
}
